﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using DoAnWeb.DTO;

namespace DoAnWeb.DAO
{
    public class DonHangDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<DonHangDTO> GetList_KhachHang(string _TenTaiKhoan)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@TenTaiKhoan", _TenTaiKhoan) };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_DonHang_KhachHang", sqlParameter);
                dp.CloseConnect();
                BindingList<DonHangDTO> List = new BindingList<DonHangDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    DonHangDTO DTO = new DonHangDTO();
                    DTO.MaDonHang = int.Parse(row["MaDonHang"].ToString());
                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.NgayDat = DateTime.Parse(row["NgayDat"].ToString());
                    DTO.TongTien = int.Parse(row["TongTien"].ToString());
                    DTO.SoLoai = int.Parse(row["SoLoai"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());

                    if (int.Parse(row["DaGiao"].ToString()) == 0)
                        DTO.DaGiao = "Đã giao";
                    else
                        DTO.DaGiao = "Chưa giao";
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                        DTO.DaXoa = "Bình thường";
                    else
                        DTO.DaXoa = "Đã Xóa";
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<DonHangDTO> GetFromID(string _MaDonHang)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaDonHang", _MaDonHang) };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetFromID_DonHang", sqlParameter);
                dp.CloseConnect();
                BindingList<DonHangDTO> List = new BindingList<DonHangDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    DonHangDTO DTO = new DonHangDTO();
                    DTO.MaDonHang = int.Parse(row["MaDonHang"].ToString());
                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.NgayDat = DateTime.Parse(row["NgayDat"].ToString());
                    DTO.TongTien = int.Parse(row["TongTien"].ToString());
                    DTO.SoLoai = int.Parse(row["SoLoai"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());

                    if (int.Parse(row["DaGiao"].ToString()) == 0)
                        DTO.DaGiao = "Đã giao";
                    else
                        DTO.DaGiao = "Chưa giao";
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                        DTO.DaXoa = "Bình thường";
                    else
                        DTO.DaXoa = "Đã Xóa";
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public bool CheckDaGiao(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaDonHang", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_CheckDaGiao_DonHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(DonHangDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@DiaChi", _DTO.DiaChi), 
                    new SqlParameter("@DienThoai", _DTO.DienThoai) ,
                    new SqlParameter("@HoTen", _DTO.HoTen),
                    new SqlParameter("@LoiNhan", _DTO.LoiNhan),
                    new SqlParameter("@TenTaiKhoan", _DTO.TenTaiKhoan)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_DonHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public int GetMaxID()
        {
            if (dp.OpenConnect())
            {
                object kq = dp.SqlExcuteScalar("uproc_GetMaxID_DonHang", null);
                dp.CloseConnect();
                return Convert.ToInt32(kq);
            }
            return -1;
        }

    }
}